package com.ledim.service;

import com.ledim.bean.LedimEpisodeBean;
import com.ledim.bean.RoomAddPlayListResponse;
import com.ledim.bean.RoomInfoResponse;
import com.ledim.bean.RoomPlaylistResponse;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.ToastUtil;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ccx on 16/9/12.
 * 网络请求Utils
 */
public class LedimApiServiceUtils {

    private static final int POSITIVE = 180001;
    private static final int TV = 181031;

    /**
     * 取消关注接口和关注接口
     *
     * @param requestFlag
     * @param uid
     * @param observer
     * @return
     */
    public static Subscription requestFanAndFollowData(int requestFlag, String uid,
                                                       LedimHttpObserver observer) {
        if (ConstantUtil.NOT_FOCUS_STATE == requestFlag) {
            //请求关注接口
            return LedimApiServiceFactory.createService(ApiService.class)
                                         .followFocus(uid)
                                         .observeOn(AndroidSchedulers.mainThread())
                                         .subscribeOn(Schedulers.io())
                                         .subscribe(observer);

        } else if (ConstantUtil.ALREADY_FOCUS_STATE == requestFlag ||
                ConstantUtil.TOGETHER_FOCUS_STATE == requestFlag) {
            //请求取消关注接口
            return LedimApiServiceFactory.createService(ApiService.class)
                                         .cancleFollow(uid)
                                         .observeOn(AndroidSchedulers.mainThread())
                                         .subscribeOn(Schedulers.io())
                                         .subscribe(observer);
        }
        return null;
    }

    /**
     * 关注失败或者取消关注失败
     */
    public static void followFailMsg(int relation) {
        if (relation == ConstantUtil.NOT_FOCUS_STATE) {
            ToastUtil.toast("关注失败");
        } else {
            ToastUtil.toast("取消关注失败");
        }
    }

    /**
     * @param observer
     * @return 获取我的room信息
     */
    public static Subscription getMyRoomInfo(LedimHttpObserver<RoomInfoResponse> observer) {
        return LedimApiServiceFactory.createService(ApiService.class)
                                     .getMyRoomInfo()
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);

    }

    /**
     * @param roomId
     * @param observer
     * @return 获取room播放列表
     */
    public static Subscription getRoomPlayList(String roomId,
                                               LedimHttpObserver<RoomPlaylistResponse> observer) {
        return LedimApiServiceFactory.createService(ApiService.class)
                                     .getRoomPlayList(roomId)
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);

    }

    /**
     * @param roomId
     * @param episodes
     * @param observer
     * @return 获取room播放列表
     */
    public static Subscription addRoomPlayList(String roomId, ArrayList<LedimEpisodeBean> episodes,
                                               LedimHttpObserver<RoomAddPlayListResponse> observer) {

        String media = null;
        String submedias = null;
        try {
            JSONArray array = new JSONArray();

            if (episodes != null && episodes.size() > 0) {
                media = episodes.get(0).pid + "";
                for (int i = 0; i < episodes.size(); i++) {
                    JSONObject obj1 = new JSONObject();
                    LedimEpisodeBean bean = episodes.get(i);
                    obj1.put("title", bean.nameCn);
                    obj1.put("duration", bean.duration + "");
                    obj1.put("submedia", bean.id + "");
                    obj1.put("btime", bean.btime);
                    obj1.put("etime", bean.etime);
                    array.put(obj1);
                }
            }
            submedias = array.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return LedimApiServiceFactory.createService(ApiService.class)
                                     .addRoomPlayList(roomId, media, submedias)
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);

    }

    /**
     * @param roomId
     * @param media
     * @param vid
     * @param title
     * @param duration
     * @param observer
     * @return 获取room播放列表
     */
    public static Subscription addRoomPlayListByVid(String roomId, String media, String vid,
                                                    String title, String duration,
                                                    LedimHttpObserver<RoomAddPlayListResponse> observer) {

        String submedias = null;
        try {
            JSONArray array = new JSONArray();
            JSONObject obj1 = new JSONObject();
            obj1.put("title", title);
            obj1.put("duration", duration);
            obj1.put("submedia", vid);
            obj1.put("btime", 0);
            obj1.put("etime", 0);
            array.put(obj1);
            submedias = array.toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return LedimApiServiceFactory.createService(ApiService.class)
                                     .addRoomPlayList(roomId, media, submedias)
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);

    }

    /**
     * @param roomId
     * @param status
     * @param observer
     * @return 修改放映厅状态 1 开启 0关闭
     */
    public static Subscription modifyRoomStatus(String roomId, String status,
                                                LedimHttpObserver observer) {
        return LedimApiServiceFactory.createService(ApiService.class)
                                     .modifyRoomStatus(roomId, status)
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);
    }

    /**
     * @param roomId
     * @param name
     * @param info
     * @param observer
     * @return 修改Room name 或者Des
     */
    public static Subscription modifyRoomInfo(String roomId, String name, String info,
                                              LedimHttpObserver observer) {
        return LedimApiServiceFactory.createService(ApiService.class)
                                     .modifyRoomInfo(roomId, name, info)
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);
    }

    /**
     * @param channel_id
     * @param q
     * @param page
     * @param observer
     * @return 获取搜索结果
     */
    public static Subscription getSearchResult(String channel_id, String q, int page, int type,
                                               LedimHttpObserver observer) {
        return LedimApiServiceFactory.createService(ApiService.class)
                                     .getSearchResultList(channel_id, q, page, 20, type)
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);
    }

    /**
     * 获取媒资的图文，数字剧集列表
     *
     * @param media
     * @param submedia
     * @param type
     *         选填 1全部  2正片 3非正片  默认是全部
     * @param desc
     *         选填 排序 0升序 1降序
     * @param per_page
     * @param page
     * @return
     */
    public static Subscription getMediaLists(String media, String submedia, int per_page, int desc,
                                             int type, int page, LedimHttpObserver observer) {
        String mediaType = null;
        if (type != 0) {
            mediaType = String.valueOf(type);
        }
        String pageIndex = page != 0 ? String.valueOf(page) : null;
        return LedimApiServiceFactory.createService(ApiService.class)
                                     .getMediaLists(media, submedia, mediaType, desc, per_page, pageIndex)
                                     .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeOn(Schedulers.io())
                                     .subscribe(observer);
    }

    /**
     * 过滤掉非正片的信息
     */
    public static ArrayList<LedimEpisodeBean> getEpisodeDataLists(
            ArrayList<LedimEpisodeBean> episodes) {
        ArrayList<LedimEpisodeBean> data = new ArrayList<LedimEpisodeBean>();
        for (int i = 0; i < episodes.size(); i++) {
            if (episodes.get(i).videoType != null) {
                switch (episodes.get(i).getVedioType()) {
                    case POSITIVE://正片
                    case TV://TV版
                        data.add(episodes.get(i));
                        break;
                    default:
                        break;
                }
            }
        }
        return data;

    }

}
